$(function () {
    tabCloseEven();
    //当前选中的节点
    var currentNode = null;
    
    //加载树节点
    $('#rightTree').tree({
    	url:'rightTree.json',
        animate:true,
        lines:true,
        onClick:function(node){
    		$(this).tree('toggle',node.target);
        },
        onSelect:function (node) {
            if (currentNode != null && node.id == currentNode.id) return;
            if (node.attributes == null) return;
            var url = node.attributes.url;
            var title = node.text;
            addTab(title, url);
        }
    });
    
    $('#changePassword').click(function(){
    	 $('#updatePasswordDialog').dialog('open');
    })
    
    $('#exitSystem').click(function(){
    	$.messager.confirm('提示','确定要退出系统吗?',function(r){
    		if(r){
    			asyncCallService('login!logout.action',null,function(result){
    				window.location.href='login.html';
    			});
    		}
    	})
    })
    function addTab(title, url) {
        if ($('#tabs').tabs('exists', title)) {
            $('#tabs').tabs('select', title);//选中
            return;
        } else {
            var panelInfo = {
                title:title,
                closable:true,
                href:url, border:false, plain:true,
                extractor:function (d) {
                    if (!d) {
                        return d;
                    }
                    if (window['CSIT']) {
                        var id = CSIT.genId();
                        return d.replace(/\$\{id\}/g, id);
                    }
                    return d;
                },
                onLoad:function (panel) {
                    var tab = $('.plugins', this);
                    if ($(tab).size() == 0) {
                        return;
                    }
                    (window['CSIT'] && CSIT.initContent && CSIT.initContent(this));
                }
            };
            $('#tabs').tabs('add', panelInfo);
        }
        tabClose();
    }

    function tabClose() {
        /*双击关闭TAB选项卡*/
        $(".tabs-inner").dblclick(function () {
            var subtitle = $(this).children(".tabs-closable").text();
            $('#tabs').tabs('close', subtitle);
        })
        /*为选项卡绑定右键*/
        $(".tabs-inner").bind('contextmenu', function (e) {
            $('#mm').menu('show', {
                left:e.pageX,
                top:e.pageY
            });

            var subtitle = $(this).children(".tabs-closable").text();

            $('#mm').data("currtab", subtitle);
            $('#tabs').tabs('select', subtitle);
            return false;
        });
    }

    //绑定右键菜单事件
    function tabCloseEven() {
        //关闭当前
        $('#mm-tabclose').click(function () {
            var currtab_title = $('#mm').data("currtab");
            $('#tabs').tabs('close', currtab_title);
        })
        //全部关闭
        $('#mm-tabcloseall').click(function () {
            $('.tabs-inner span').each(function (i, n) {
                var t = $(n).text();
                if (t != '首页') {
                    $('#tabs').tabs('close', t);
                }
            });
        });
        //关闭除当前之外的TAB
        $('#mm-tabcloseother').click(function () {
            var prevall = $('.tabs-selected').prevAll();
            var nextall = $('.tabs-selected').nextAll();
            if (prevall.length > 0) {
                prevall.each(function (i, n) {
                    var t = $('a:eq(0) span', $(n)).text();
                    if (t != '首页') {
                        $('#tabs').tabs('close', t);
                    }
                });
            }
            if (nextall.length > 0) {
                nextall.each(function (i, n) {
                    var t = $('a:eq(0) span', $(n)).text();
                    if (t != '首页') {
                        $('#tabs').tabs('close', t);
                    }
                });
            }
            return false;
        });

        //退出
        $("#mm-exit").click(function () {
            $('#mm').menu('hide');
        })
        
        var onSave = function(){
        	var oldPassword = $('#oldPassword').val();
        	if($.trim(oldPassword)==''){
        		$.messager.alert('警告','旧密码不能为空','warning');
        		return;
        	}
        	var password = $('#password').val();
        	if($.trim(password)==''){
        		$.messager.alert('警告','新密码不能为空','warning');
        		return;
        	}
        	var confirmpassword = $('#confirmpassword').val();
        	if($.trim(password)!=$.trim(confirmpassword)){
        		$.messager.alert('警告','两次密码不一致','warning');
        		return;
        	}
        	var url = 'user!updatePassword.action';
        	var content = {oldPassword:oldPassword,password:password};
        	asyncCallService(url,content,function(result){
        		if(result.success){
        			$.messager.alert('警告','修改密码成功','info',onExit);
        			return;
        		}else{
        			$.messager.alert('警告',result.msg,'warning');
        		}
        	})
        }
        
        var onExit = function(){
        	$('#passwordForm').form('clear');
        	$('#updatePasswordDialog').dialog('close');
        }
        
        $('#updatePasswordDialog').dialog({  
		    title: '修改密码',  
		    width:400,
		    height:250,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
    }
})